package com.jackrain.nea.fc.dl.service;


import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.fc.dl.api.WmsBackWriteSaveBillNoCmd;
import com.jackrain.nea.fc.dl.mapper.WmsBackWriteBillNoMapper;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ValueHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * WMS回写
 *
 * @author zhang.zz
 * @date 2018/12/07
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "dl")
public class WmsBackWriteSaveBillNoCmdImpl extends CommandAdapter implements WmsBackWriteSaveBillNoCmd {


    @Override
    public ValueHolder execute(HashMap map) throws NDSException {

//        log.debug("WmsBackWriteSaveBillNoCmdImpl-->start");
        ValueHolder vh = new ValueHolder();
        WmsBackWriteBillNoMapper wmsBackWriteBillNoMapper = ApplicationContextHandle.getBean(WmsBackWriteBillNoMapper.class);
        JSONObject param = JSON.parseObject(map.get("param").toString());
        log.info("WmsBackWriteSaveBillNoCmdImpl--param-->{}",param);

        if (null == param || (!param.containsKey("IDS"))) {
            throw new NDSException("参数格式错误！");
        }
        String tablename = param.getString("tablename");

        String ids = param.getString("IDS");
        Object[] objects = ids.split(",");
        log.debug("WmsBackWriteSaveBillNoCmdImpl-objects--->{}", objects);
        int updateRets = 0;
        if (tablename.equals("DL_B_PUR_ORDER")) {
            updateRets = wmsBackWriteBillNoMapper.updatorderwmsState(ids);
        } else if (tablename.equals("DL_B_TRAN")) {//提交
            updateRets = wmsBackWriteBillNoMapper.updatwmssubmitState(objects);
        } else if (tablename.equals("DL_B_TRAN_OUT")) {//发出
            updateRets = wmsBackWriteBillNoMapper.updatwmssendState(ids);
        } else if (tablename.equals("DL_B_PUR_IN_ACCEPT")) {//采购入库验收
            updateRets = wmsBackWriteBillNoMapper.updatpurinwmsState(ids);
        } else if (tablename.equals("DL_B_PUR_RET_SEND")) {//采购退货发出
            updateRets = wmsBackWriteBillNoMapper.updatpurretwmsState(ids);
        } else if (tablename.equals("DL_B_TRAN_OUT_SEND")) {//调拨出库发出
            updateRets = wmsBackWriteBillNoMapper.updattranoutwmsState(ids);
        } else if (tablename.equals("DL_B_TRAN_IN_ACCEPT")) {//调拨入库验收
            updateRets = wmsBackWriteBillNoMapper.updattraninwmsState(ids);
        } else if (tablename.equals("TRAN_BOX_SEND")) {//调拨入库验收
            updateRets = wmsBackWriteBillNoMapper.updatetranboxwmsState(objects);
            List<Long> idList = wmsBackWriteBillNoMapper.findIdListByBillNo(objects);
            log.debug("WmsBackWriteSaveBillNoCmdImpl-idList--->{}", idList);
            Object[] idArray = idList.toArray();
            wmsBackWriteBillNoMapper.updatetranboxwmsState1(idArray);
        }

        if (updateRets <= 0) {
            throw new NDSException("WMS标示更新失败！");
        }
        vh.put("code", 0);
        vh.put("message", "更新成功！");
        return vh;

    }


}
